#include "Mychattool.h"

int main(int argc, char **argv)
{
    int i = 0;
    int sockfd, newfd;
    struct sockaddr_in s_addr, c_addr;
    char buf[BUFLEN];
    socklen_t len;
    unsigned int port, listnum;

    time_t timer;//time_t就是long int 类型
    struct tm *tblock;

    /*创建客户端套接字*/
    if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
        print_time(timer,&tblock);
        perror("socket");
        exit(errno);
    }else
        printf("socket create success!\n");

    /*设置服务器ip*/
    bzero(&s_addr, sizeof(s_addr));
    s_addr.sin_family = AF_INET;
    s_addr.sin_port = htons(SERVER_PORT);
    s_addr.sin_addr.s_addr = inet_addr("127.0.0.1");

    /*把地址和端口帮定到套接字上*/
    if((bind(sockfd, (struct sockaddr*) &s_addr,sizeof(struct sockaddr))) == -1){
        print_time(timer,&tblock);
        perror("bind");
        exit(errno);
    }else{
        print_time(timer,&tblock);
        printf("bind success!\n");
    }
    /*侦听本地端口*/
    if(listen(sockfd,listnum) == -1){
        print_time(timer,&tblock);
        perror("listen");
        exit(errno);
    }else {
        printf("loading!\n");
        sleep(1);
        printf(">>>>>>>>>>>>>>>\n\n");
    }

    while(1){
        printf("|***************** server——waiting ***************|\n");
        len = sizeof(struct sockaddr);
        if((newfd = accept(sockfd,(struct sockaddr*) &c_addr, &len)) == -1){
            print_time(timer,&tblock);
            perror("accept");
            exit(errno);
        }else
            printf("chatting with client：IP: %s PORT: %d\n",inet_ntoa(c_addr.sin_addr),ntohs(c_addr.sin_port));
            //inet_ntoa()将IP地址在“点分十进制”和“整数”之间转换
        while(1){
        send_r:
            /******发送消息*******/
            bzero(buf,BUFLEN);
            print_time(timer,&tblock);
            printf("server:>");
            /*fgets函数：从流中读取BUFLEN-1个字符*/
            fgets(buf,BUFLEN,stdin);
            printf("\n" );
            //strncasecmp()用来比较参数s1和s2字符串前n个字符，忽略大小写的差异。
            if(!strncasecmp(buf,"quit",4)){
                print_time(timer,&tblock);
                printf("server 请求终止聊天!\n\n");
                bzero(buf,BUFLEN);
                strcpy (buf,"服务器请求终止聊天：y->是；n->否?\n ");
                //printf("给 client 说再见吧!\n");
                //printf("say_goodbye:>");
                //fgets(buf,BUFLEN,stdin);
            }
            /*如果输入的字符串只有"\n"，即回车，那么请重新输入*/
            if(!strncmp(buf,"\n",1)){
                print_time(timer,&tblock);
                printf("输入的字符只有回车，这个是不正确的！！！\n\n");
                goto send_r;
            }
            /*如果buf中含有'\n'，那么要用strlen(buf)-1，去掉'\n'*/
            if(strchr(buf,'\n'))
                len = send(newfd,buf,strlen(buf)-1,0);
            /*如果buf中没有'\n'，则用buf的真正长度strlen(buf)*/
            else
                len = send(newfd,buf,strlen(buf),0);
            if(len < 0)
            {
                printf("消息发送失败!\n\n");
                break;
            }

            /*接收消息*/
            bzero(buf,BUFLEN);
            len = recv(newfd,buf,BUFLEN,0);
            if(len > 0) {
                print_time(timer,&tblock);

                if(!strncasecmp(buf,"emo",3)){
                    printf("clien:> ");
                    emo_out(buf);
                    printf("\n\n" );
                }
                else
                    printf("clien:>%s \n\n",buf);
                if(!strncasecmp(buf,"y",1)){
                    print_time(timer,&tblock);
                    printf("客户端同意终止聊天！我们再会~\n\n");
                    break;
                }

            }

            else{
                if(len < 0 ) {
                    print_time(timer,&tblock);
                    printf("接受消息失败！\n");
                }
                else {
                    print_time(timer,&tblock);
                    printf("客户端下线，聊天终止！\n\n");
                }
                break;
            }
        }
        /*关闭聊天的套接字*/
        close(newfd);
        /*是否退出服务器*/
        print_time(timer,&tblock);
        printf("server quit? : y->是 n->否? ");
        bzero(buf, BUFLEN);
        fgets(buf,BUFLEN, stdin);
        printf("\n");
        if(!strncasecmp(buf,"y",1)){
            print_time(timer,&tblock);
            printf("server 退出!\n");
            close(newfd);
            break;
        }
    }
    /*关闭服务器的套接字*/
    close(sockfd);
    return 0;
}
